package org.acg.common.table;
import cn.hutool.json.JSONObject;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReadConfig;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.json.JSONUtil;
import org.artifact.core.util.ParseUtil;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.HashMap;

public class JavaConfigLoader{

    public interface ConfigTabelLoader{
        LinkedHashMap getTable(Class clazz);
        void setGlobal(JSONObject global);
        void putI18n(String key,String val);
    }

    static final int CSV_HEAD_ROW_COUNT = 4;
    static CsvReader reader = null;
    static {
        CsvReadConfig config = new CsvReadConfig();
        config.setFieldSeparator(';');
        reader = new CsvReader(config);
    }




    public static void load(String path,ConfigTabelLoader loader) {


        // 读取离散文件
        String globalContext = FileUtil.readString(path + "Global.json", CharsetUtil.CHARSET_UTF_8);
        loader.setGlobal(JSONUtil.parseObj(globalContext));

        // 读取国际化文件
        CsvData I18nRows = reader.read(FileUtil.file(path + "I18n.csv"),CharsetUtil.CHARSET_UTF_8);
        for (int i = CSV_HEAD_ROW_COUNT; i < I18nRows.getRowCount(); i++) {
            CsvRow row = I18nRows.getRow(i);
            loader.putI18n(row.get(0), row.get(1));
        }

        // 技能数据
        CsvData ConfigSkillDataRows = reader.read(FileUtil.file(path + "ConfigSkillData.csv"),CharsetUtil.CHARSET_UTF_8);
        for (int i = CSV_HEAD_ROW_COUNT; i < ConfigSkillDataRows.getRowCount(); i++) {
            CsvRow row = ConfigSkillDataRows.getRow(i);
            ConfigSkillDataConfig o = new ConfigSkillDataConfig(row);
            loader.getTable(ConfigSkillDataConfig.class).put(o.getId(), o);
        }

    }
}